﻿Imports System.Text
Imports TwitterAPI
Imports VsSpeakerCompanion
Imports System.Windows
Imports Microsoft.VisualStudio.Text.Editor
Imports System.ComponentModel.Composition
Imports System.Windows.Input
Imports VsSpeakerCompanion.UI

Namespace TwitterThisWidget
    ''' <summary>
    ''' Allows you to select some text in the code editor and send it using Twitter
    ''' </summary>
    Friend Class Widget
        Implements IWidget
        ' our widget UI
        Private WithEvents view As TwitterThisView

        ' services we will be making use of
        ' for sending Twitter messages
        Private twitterService As TwitterService
        ' for tracking the active text view
        Private visualStudioService As IVisualStudioService
        ' to show messages to the user
        Private uiService As IUIService

        Public Sub New(ByVal twitterService As TwitterService, ByVal visualStudioService As IVisualStudioService, ByVal uiService As IUIService)
            Me.twitterService = twitterService
            Me.visualStudioService = visualStudioService
            Me.uiService = uiService

            Me.view = New TwitterThisView
        End Sub

        Public ReadOnly Property VisualElement As UIElement Implements IWidget.VisualElement
            Get
                ' return our TweetThis! UI
                Return view
            End Get
        End Property

        Public Sub Reset() Implements IWidget.Reset
            ' this widget doesn't use any state that may need to be reset
        End Sub

        Private Sub UpdateStatus(ByVal newStatus As String)
            ' This uses the Twitter service to update the speaker Twitter status publishing the selected code
            Me.uiService.ExecuteOperation(Sub() twitterService.UpdateStatus(twitterService.GetMessagesWithHashcode(newStatus)))
        End Sub

        Private Sub OnViewTweet() Handles view.Tweet
            ' Retrieve the active text view
            Dim activeTextViewInstance = Me.visualStudioService.GetActiveTextView()

            If activeTextViewInstance IsNot Nothing Then

                ' if there is a selection at all
                If activeTextViewInstance.Selection.SelectedSpans.Count > 0 Then
                    ' Get the current selection of the active text view
                    Dim textToTweet = activeTextViewInstance.Selection.SelectedSpans(0).GetText()

                    If Not String.IsNullOrEmpty(textToTweet) Then
                        ' Tweet it!
                        ' We don't need to check the length of the selection as our Twitter APIs support the sending of large messages by
                        ' automatically splitting them in several separate messages.
                        UpdateStatus(textToTweet)
                    End If
                Else
                    ' TODO: Move text to resource file for easier localization
                    ' Tell the user it's not funny to twitter no text!
                    Me.uiService.ShowMessage("There is nothing to tweet! Please select some code and click on the widget again.")
                End If
            End If
        End Sub
    End Class
End Namespace